/**
 * @File AssetController.java
 * @author NUPUR
 * @Version 1.0
 * @Date April 11, 2013
 * 
 * Copyright (c) 2011 EdRP, Dayalbagh Educational Institute.
 * All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 *
 * Redistributions of source code must retain the above copyright
 * notice, this  list of conditions and the following disclaimer.
 *
 * Redistribution in binary form must reproducuce the above copyright
 * notice, this list of conditions and the following disclaimer in
 * the documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL ETRG OR ITS CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL,SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * Contributors: Members of EdRP, Dayalbagh Educational Institute
 */
package in.ac.dei.edrp.purchase.assetRegister;

import flex.messaging.FlexContext;
import flex.messaging.FlexSession;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AssetController extends MultiActionController {
	private static Logger logger = Logger.getLogger(AssetController.class);
	private AssetDao assetDao;
	public void setAssetDao(AssetDao assetDao){
		this.assetDao=assetDao;
	}
	
	
	public List<AssetBean> getCategories(){
		List<AssetBean> beanList = assetDao.getCategories();		
		return beanList;
	}
	
	public String setAssetDetails(Map<String,Object> mapInput){
		String result="false";
		try {	
			FlexSession session = FlexContext.getFlexSession();		
			String univCode = (String) session.getAttribute("universityCode");
			AssetBean assetBean = new AssetBean();
			assetBean.setAssetName((String) mapInput.get("assetName"));
			assetBean.setMainCatCode((String) mapInput.get("mainCatCode"));
			assetBean.setCategoryCode((String) mapInput.get("subCatCode"));
			assetBean.setAssetClassCode((String) mapInput.get("assetClassCode"));
			assetBean.setAssetSubClassCode((String) mapInput.get("assetSubClassCode"));
			assetBean.setMajorType((String) mapInput.get("majorType"));
			assetBean.setAcquisitionCode((String) mapInput.get("acquisitionCode"));
			assetBean.setDispositionCode((String) mapInput.get("dispositionCode"));
			assetBean.setStatus((String) mapInput.get("status"));  
			assetBean.setLocationCode((String) mapInput.get("location"));  
			assetBean.setPurchaseDate((String) mapInput.get("datePurchased"));   
			assetBean.setPurchaseOrderNo((String) mapInput.get("purchaseOrderNo")); 
			assetBean.setWarranty((String) mapInput.get("warranty"));  
			assetBean.setInvoice((String) mapInput.get("invoice"));   
			assetBean.setUnitCost(String.valueOf((mapInput.get("unitCost")=="" || mapInput.get("unitCost")==null?0.0:  mapInput.get("unitCost"))));  
			assetBean.setQuantity(String.valueOf((mapInput.get("quantity")=="" || mapInput.get("quantity")==null?0.0:  mapInput.get("quantity"))));  
			assetBean.setVendorName((String) mapInput.get("vendorName"));
			assetBean.setVendorContactInfo((String) mapInput.get("vendorContactInfo"));
			assetBean.setVendorAddress((String) mapInput.get("vendorAddress"));
			assetBean.setMake((String) mapInput.get("make"));
			assetBean.setModel((String) mapInput.get("model"));
			assetBean.setSerialNo((String) mapInput.get("serialNo")); 
			assetBean.setBarcode((String) mapInput.get("barcode"));
			assetBean.setLength(String.valueOf((mapInput.get("length")=="" || mapInput.get("length")==null?0.0:  mapInput.get("length"))));
			assetBean.setWidth(String.valueOf((mapInput.get("width")=="" || mapInput.get("width")==null?0.0:  mapInput.get("width"))));
			assetBean.setHeight(String.valueOf((mapInput.get("height")=="" || mapInput.get("height")==null?0.0:  mapInput.get("height"))));
			assetBean.setWeight(String.valueOf((mapInput.get("weight")=="" || mapInput.get("weight")==null?0.0:  mapInput.get("weight"))));
			assetBean.setColour(String.valueOf(mapInput.get("colour"))); 
			assetBean.setCreatorId((String) session.getAttribute("userId"));
			assetBean.setModifierId((String) session.getAttribute("userId"));
			assetBean.setUniversityCode(univCode);
			String iOrUFlag = (String) mapInput.get("iOrUFlag");		
			if(iOrUFlag.equalsIgnoreCase("update")){			
				assetBean.setAssetCode( (String) mapInput.get("assetCode"));
				result = assetDao.updateAsset(assetBean);
			}
			else{			
				result = assetDao.setAsset(assetBean);
			}				
		} catch (Exception e) {
			result="false";
			logger.error("Error inside setAssetDetails method of AssetController Class : ",e);
		}
		return result;
	}
	
	public String deleteAsset(String assetCode){
		FlexSession session = FlexContext.getFlexSession();		
		String univCode = (String) session.getAttribute("universityCode");
		AssetBean aBean = new AssetBean();
		aBean.setAssetCode(assetCode);		
		aBean.setUniversityCode(univCode);		
		String msg = assetDao.deleteAsset(aBean);		
		return msg;
	}
	
	public List<AssetBean> getMainCategory(String groupCode){	
		FlexSession session = FlexContext.getFlexSession();				
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode((String) session.getAttribute("universityCode"));
		aBean.setGroupCode(groupCode);
		List<AssetBean> assetList = assetDao.getMainCategory(aBean);		
		return assetList;
	}
	
	public List<AssetBean> getSubCategory(String mainCatCode){		
		FlexSession session = FlexContext.getFlexSession();		
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode((String) session.getAttribute("universityCode"));
		aBean.setMainCatCode(mainCatCode);
		List<AssetBean> assetList = assetDao.getSubCategory(aBean);	
		return assetList;
	}
	
	public List<AssetBean> getLocation(){		
		FlexSession session = FlexContext.getFlexSession();					
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode((String) session.getAttribute("universityCode"));
		List<AssetBean> assetList = assetDao.getLocation(aBean);		
		return assetList;
	}

	public List<AssetBean> getAllAsset(){
		FlexSession session = FlexContext.getFlexSession();		
		String univCode = (String) session.getAttribute("universityCode");
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode(univCode);
		List<AssetBean> assetList = assetDao.getAllAsset(aBean);	
		return assetList;		
	}
	
	public List<AssetBean> getAssetClass(){		
		FlexSession session = FlexContext.getFlexSession();					
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode((String) session.getAttribute("universityCode"));
		List<AssetBean> assetList = assetDao.getAssetClass(aBean);		
		return assetList;
	}

	public List<AssetBean> getAssetSubClass(String assetClassCode){
		FlexSession session = FlexContext.getFlexSession();		
		String univCode = (String) session.getAttribute("universityCode");
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode(univCode);
		aBean.setAssetClassCode(assetClassCode);
		List<AssetBean> assetList = assetDao.getAssetSubclass(aBean);	
		return assetList;		
	}
	
	public List<AssetBean> getAccounts(){
		FlexSession session = FlexContext.getFlexSession();		
		String univCode = (String) session.getAttribute("universityCode");
		AssetBean aBean = new AssetBean();
		aBean.setUniversityCode(univCode);
		List<AssetBean> assetList = assetDao.getAccounts(aBean);	
		return assetList;		
	}
}
